package org.jeecg.modules.website.websiteSetup.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.modules.websiteDetails.entity.WebsiteDetails;
import org.jeecg.common.modules.websiteDetails.service.IWebsiteDetailsService;
import org.jeecg.common.modules.websiteSetup.entity.WebsiteSetup;
import org.jeecg.common.modules.websiteSetup.service.IWebsiteSetupService;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
import org.jeecg.common.modules.websiteSetup.mapper.WebsiteSetupVO;

/**
 * @Description: 官网设置
 * @Author: jeecg-boot
 * @Date:   2021-11-29
 * @Version: V1.0
 */
@Api(tags="官网设置")
@RestController
@RequestMapping("/websiteSetup/websiteSetup")
@Slf4j
public class WebsiteSetupController extends JeecgController<WebsiteSetup, IWebsiteSetupService> {
	@Autowired
	private IWebsiteSetupService websiteSetupService;
	 @Autowired
	 private IWebsiteDetailsService websiteDetailsService;
	
//	/**
//	 * 分页列表查询
//	 *
//	 * @param websiteSetup
//	 * @param pageNo
//	 * @param pageSize
//	 * @param req
//	 * @return
//	 */
//	@AutoLog(value = "官网设置-分页列表查询")
//	@ApiOperation(value="官网设置-分页列表查询", notes="官网设置-分页列表查询")
//	@GetMapping(value = "/list")
//	public Result<?> queryPageList(WebsiteSetup websiteSetup,
//								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
//								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
//								   HttpServletRequest req) {
//		QueryWrapper<WebsiteSetup> queryWrapper = QueryGenerator.initQueryWrapper(websiteSetup, req.getParameterMap());
//		Page<WebsiteSetup> page = new Page<WebsiteSetup>(pageNo, pageSize);
//		IPage<WebsiteSetup> pageList = websiteSetupService.page(page, queryWrapper);
//		return Result.ok(pageList);
//	}
	/**
	 * 分页列表查询
	 *
	 * @param websiteSetup
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "官网设置-分页列表查询")
	@ApiOperation(value="官网设置-分页列表查询", notes="官网设置-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(WebsiteSetup websiteSetup,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		//分页数据开始的记录数dataStart
		int dataStart = (pageNo - 1) * pageSize;
		IPage<WebsiteSetupVO> pageList = new Page<>(dataStart,pageSize);
		List<WebsiteSetupVO> websiteSetupVOS =  websiteSetupService.selectListByType(websiteSetup.getType(), dataStart, pageSize);
		pageList.setRecords(websiteSetupVOS);
		return Result.ok(pageList);
	}
	/**
	 *   添加
	 *
	 * @param websiteSetupVO
	 * @return
	 */
	@AutoLog(value = "官网设置-添加")
	@ApiOperation(value="官网设置-添加", notes="官网设置-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody WebsiteSetupVO websiteSetupVO) {
		//type 0-广告，1-公告，2-文章
		String uid = UUID.randomUUID().toString().replace("-", "");
		WebsiteSetup websiteSetup = new WebsiteSetup();
		WebsiteDetails websiteDetails = new WebsiteDetails();
		BeanUtils.copyProperties(websiteSetupVO,websiteSetup);
		BeanUtils.copyProperties(websiteSetupVO,websiteDetails);
		SimpleDateFormat sdfYear = new SimpleDateFormat("yyyy");
		SimpleDateFormat sdfMouth = new SimpleDateFormat("MM");
		SimpleDateFormat sdfDay = new SimpleDateFormat("dd");
		//官网设置
		websiteSetup.setId(uid);
		websiteSetup.setYear(sdfYear.format(websiteSetupVO.getDate()));
		websiteSetup.setMouth(sdfMouth.format(websiteSetupVO.getDate()));
		websiteSetup.setDay(sdfDay.format(websiteSetupVO.getDate()));
		websiteSetupService.save(websiteSetup);
		if(websiteDetails.getContent()!= null ){
			//官网详情
			websiteDetails.setId(uid);
			websiteDetailsService.save(websiteDetails);
		}
		return Result.ok("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param websiteSetup
	 * @return
	 */
	@AutoLog(value = "官网设置-编辑")
	@ApiOperation(value="官网设置-编辑", notes="官网设置-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody WebsiteSetupVO websiteSetupVO) {
		WebsiteSetup websiteSetup = new WebsiteSetup();
		WebsiteDetails websiteDetails = new WebsiteDetails();
		BeanUtils.copyProperties(websiteSetupVO,websiteSetup);
		BeanUtils.copyProperties(websiteSetupVO,websiteDetails);
		websiteSetupService.updateById(websiteSetup);
		//官网详情
		websiteDetails.setId(websiteSetup.getId());
		websiteDetailsService.updateById(websiteDetails);
		return Result.ok("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "官网设置-通过id删除")
	@ApiOperation(value="官网设置-通过id删除", notes="官网设置-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		websiteSetupService.removeById(id);
		return Result.ok("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "官网设置-批量删除")
	@ApiOperation(value="官网设置-批量删除", notes="官网设置-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.websiteSetupService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.ok("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "官网设置-通过id查询")
	@ApiOperation(value="官网设置-通过id查询", notes="官网设置-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		WebsiteSetupVO websiteSetupVO = new WebsiteSetupVO();
		WebsiteSetup websiteSetup = websiteSetupService.getById(id);
		if(websiteSetup==null) {
			return Result.error("未找到对应数据");
		}
		WebsiteDetails websiteDetails = websiteDetailsService.getById(id);
		BeanUtils.copyProperties(websiteSetup,websiteSetupVO);
		websiteSetupVO.setContent(websiteDetails!= null? websiteDetails.getContent():null);
		return Result.ok(websiteSetupVO);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param websiteSetup
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, WebsiteSetup websiteSetup) {
        return super.exportXls(request, websiteSetup, WebsiteSetup.class, "官网设置");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, WebsiteSetup.class);
    }

}
